import Foundation
import Eelay
import UIKit
import JoLoading

@objc open class JoDefaultLoading: JoLoadingBase {
    
    @objc required public init() {
        super.init()
        self.addLayoutRules()
    }
    
    
    
    
    @objc required public init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    public let solutionL = UIButton()
    
    public let contentView = UIView()
    
    public let stackView = UIStackView()
    
    override open func addLayoutRules() {
        //        self.eelay = [
        //            [imageV,[ee.X.Y,[0,-30]],[ee.X],Int(10),"\(10)"],
        //            [titleL,[imageV,ee.B,ee.T,-10],[ee.L.R,[20,-20]]],
        //            [infoL,[ee.X],[titleL,ee.B,ee.T,8],[ee.L.R,[20,-20]]],
        //            [indicator,[ee.X],[infoL,ee.B,ee.T,16]],
        //            [button,[ee.X],[infoL,ee.B,ee.T,16]],
        //            [solutionL,[ee.X],[button,ee.B,ee.T,8]],
        //        ]
        
        let contentView = UIView()
        
        contentView.eelay = [
            [stackView,[ee.X.Y],(Swidth-2.0*20)],
        ]
        eelay = [
            [contentView,[ee.T.L.B.R.safe]]
        ]
        
        stackView.axis = .vertical
        stackView.alignment = .center
        stackView.spacing = 10
        //        self.eelay = [
        //            [indicator,[ee.X.Y]],
        //            //            [titleL,[indicator,ee.B,ee.T,10],[ee.X],[ee.L.R,[20,-20]]],
        //            [infoL,[ee.X],[indicator,ee.B,ee.T,10],[ee.L.R,[20,-20]]],
        //            [button,[ee.X],[infoL,ee.B,ee.T,16]],
        //            [solutionL,[ee.X],[button,ee.B,ee.T,8]],
        //        ]
        
        //        solutionL.setTitle("查看解决方案", for: .normal)
        //        solutionL.setTitleColor(.blue, for: .normal)
        _ = solutionL.titleLabel?.ui.font14
        
        button.ui.font(size: 14).ncolor(color:.main_color).set(radius: 15, width: 1, color: .main_color)
        
        button.addTarget(self, action: #selector(handleAction), for: .touchUpInside)
        button.tag = 1
        button.contentEdgeInsets =  [6,40,6,40]
        indicator.style = UIActivityIndicatorView.Style.gray
        
        //        imageV.image = #imageLiteral(resourceName: "iwe_white_holder")
        imageV.contentMode = .scaleAspectFit
        _ = infoL.ui.font14.cl_888.center.text("")
        _ = titleL.ui.font15.cl_444.center.text("")
        self.backgroundColor = ._background
        solutionL.addTarget(self, action: #selector(doFunc), for: .touchUpInside)
    }
    
    open func removeAll(){
        if titleL.superview == stackView
        {
            stackView.removeArrangedSubview(titleL)
        }
        if infoL.superview == stackView
        {
            stackView.removeArrangedSubview(infoL)
        }
        if indicator.superview == stackView
        {
            stackView.removeArrangedSubview(indicator)
        }
        if button.superview == stackView
        {
            stackView.removeArrangedSubview(button)
        }
        if solutionL.superview == stackView
        {
            stackView.removeArrangedSubview(solutionL)
        }
    }
    
    @objc open func doFunc() {
        //        let ctr = RootList()
        //        ctr.title = "gooo"
        //        self.containerController?.navigationController?.pushViewController(ctr, animated: true)
    }
    
    override open func loading(message:String,title:String)
    {
        removeAll()
        if title.len > 0
        {
            stackView.addArrangedSubview(titleL)
        }
        if message.len > 0
        {
            stackView.addArrangedSubview(infoL)
        }
        stackView.addArrangedSubview(indicator)
        
        super.loading(message: message, title: title)
        self.infoL.text = ("\(message)...")
        self.solutionL.isUserInteractionEnabled = false
        self.solutionL.alpha = 0
    }
    
    
    override open func show(message:String,title:String)
    {
        removeAll()
        
        if title.len > 0
        {
            stackView.addArrangedSubview(titleL)
        }
        if message.len > 0
        {
            stackView.addArrangedSubview(infoL)
        }
        
        super.show(message: message, title: title)
        
        self.solutionL.isUserInteractionEnabled = false
        self.solutionL.alpha = 0
    }
    
    override open func handle(message:String,title:String,button:String,handAction:@escaping ()->Void)
    {
        removeAll()
        
        super.handle(message: message, title: title, button: button, handAction: handAction)
        if title.len > 0
        {
            stackView.addArrangedSubview(titleL)
        }
        if message.len > 0
        {
            stackView.addArrangedSubview(infoL)
        }
        //        self.solutionL.isUserInteractionEnabled = true
        //        self.solutionL.alpha = 1
        //        solutionL.backgroundColor = .red
        if button.len > 0{
            stackView.addArrangedSubview(self.button)
            self.button.alpha = 1

        }else{
            self.button.alpha = 0
        }        
        //        print("------UUUUUUUUUUUU==\(stackView.arrangedSubviews)")
        
    }
    
    override open func dismiss(animated:Bool=true)
    {
        super.dismiss(animated: animated)
    }
    
    deinit {
    }
}

open class FlatLoading: JoDefaultLoading {
    
    open override func addLayoutRules() {
        super.addLayoutRules()
        
        var hex = AppTheme.main_hex
        if hex.len > 6{
            hex = hex + "88"
        }else{
            hex = hex + "8"
        }
        button.ui.font(size: 16).ncolor(hex: "#fff").buttonColor(hex: hex).set(radius: 5, width: 0, color: UIColor(shex: hex))
    }
    
}


open class SimpleLoading: JoLoadingBase {
    
    
    public let contentView = UIView()
    override open func addLayoutRules() {
        
        self.eelay = [
            [indicator,[ee.X.Y]],
            [infoL,[indicator,ee.T,ee.B,-6],[ee.L.R,[.>20.+1000,.<(-20).+1000]],[ee.X]],
            [button,[ee.X],[infoL,ee.B,ee.T,16]],
        ]
        //        solutionL.setTitle("查看解决方案", for: .normal)
        //        solutionL.setTitleColor(.blue, for: .normal)
        
        button.ui.font(size: 15).buttonColor(color: .color(light: "#8883", dark: "#ccc2")).ncolor(color: .color(light: "#444", dark: "fffc")).set(radius: 5, width: 0, color: nil)
        
        button.addTarget(self, action: #selector(handleAction), for: .touchUpInside)
        button.tag = 1
        button.contentEdgeInsets =  [6,40,6,40]
        indicator.style = MoTheme.global.mode.indicatorStyle
        
        infoL.ui.text(color:.color(light: "#999", dark: "#ccc9"))
        
        //        imageV.image = #imageLiteral(resourceName: "iwe_white_holder")
        imageV.contentMode = .scaleAspectFit
        _ = infoL.ui.font14.center.text("")
        _ = titleL.ui.font15.center.text("")
        self.backgroundColor = ._background
    }
    
    
    @objc func doFunc() {
        //        let ctr = RootList()
        //        ctr.title = "gooo"
        //        self.containerController?.navigationController?.pushViewController(ctr, animated: true)
    }
    
    override open func loading(message:String,title:String)
    {
        super.loading(message: message, title: title)
        self.infoL.text = ("\(message)")
    }
    
    
    override open func show(message:String,title:String)
    {
        super.show(message: message, title: title)
    }
    
    
    override open func dismiss(animated:Bool=true)
    {
        super.dismiss(animated: animated)
    }
    
    deinit {
    }
}








